var editingRowIndex;
var isSelectedAll;
var menuToolBar;
$(function() {
	menuToolBar = [
			{
				id : "menu_select_all",
				text : "",
				plain : true,
				iconCls : "icon-ok",
				handler : function() {
					if (isSelectedAll) {
						$("#menu_table").datagrid("unselectAll");
						isSelectedAll = false;
					} else {
						$("#menu_table").datagrid("selectAll");
						isSelectedAll = true;
					}
				}
			},
			"-",
			{
				id : "menu_add",
				text : "添加",
				plain : true,
				iconCls : "icon-add",
				handler : function() {
					if (editingRowIndex == undefined) {
						$("#menu_table").datagrid("insertRow", {
							index : 0,
							row : {}
						});
						$("#menu_table").datagrid("unselectAll");
						editingRowIndex = 0;
						$("#menu_table").datagrid("beginEdit", 0);
					}
				}
			},
			{
				id : "menu_remove",
				text : "删除",
				plain : true,
				iconCls : "icon-remove",
				handler : function() {
					if (editingRowIndex == undefined) {
						var selections = $("#menu_table").datagrid(
								"getSelections");
						if (selections.length > 0) {
							showConfim("是否执行删除操作？", function(r) {
								if (r) {
									var ids = new Array();
									$.each(selections, function(index, data) {
										ids[index] = data.id;
									});
									var rowIds = "";
									$.each(ids, function(index, value) {
										if (ids.length == (index + 1))
											rowIds += value;
										else
											rowIds += (value + ",");
									});
									$.post("manage/removeMenus.html", {
										ids : rowIds
									}, function(data) {
										if (data) {
											showMessage(data.msg);
											if (data.success) {
												$("#menu_table").datagrid(
														"load");
											}
										}
									}, "json");
								}
							});
						} else {
							showAlert("请选择要删除的数据！");
						}
					}
				}
			},
			"-",
			{
				id : "menu_save",
				text : "保存",
				plain : true,
				iconCls : "icon-save",
				handler : function() {
					if (editingRowIndex == undefined) {
						showAlert("没有需要保存的数据！");
					} else {
						if ($("#menu_table").datagrid("validateRow",
								editingRowIndex)) {
							showConfim(
									"是否执行保存操作？",
									function(r) {
										if (r) {
											var rowData = $("#menu_table")
													.datagrid("getRows")[editingRowIndex];
											$("#menu_table").datagrid(
													"endEdit", editingRowIndex);
											editingRowIndex = undefined;
											delete rowData["parentMenu"];
											$
													.post(
															"manage/saveMenu.html",
															rowData,
															function(data) {
																if (data) {
																	showMessage(data.msg);
																	if (data.success) {
																		$(
																				"#menu_table")
																				.datagrid(
																						"reload");
																	}
																}
															}, "json");
										}
									});
						}
					}
				}
			}, {
				id : "menu_undo",
				text : "取消",
				plain : true,
				iconCls : "icon-undo",
				handler : function() {
					if (editingRowIndex != undefined) {
						$("#menu_table").datagrid("rejectChanges");
						editingRowIndex = undefined;
					}
					$("#menu_table").datagrid("unselectAll");
					isSelectedAll = false;
				}
			} ];
	$("#menu_table").datagrid({
		toolbar : menuToolBar,
		fit : true,
		idField : "id",
		rownumbers : true,
		url : "manage/listMenus.html",
		pagination : true,
		pageSize : 10,
		pageList : [ 10, 20, 50, 100, 200, 500 ],
		sortName : "id",
		sortOrder : "desc",
		nowrap : false,
		fitColumns : true,
		onDblClickRow : function(inx) {
			if (editingRowIndex == undefined) {
				$("#menu_table").datagrid("unselectAll");
				editingRowIndex = inx;
				$("#menu_table").datagrid("beginEdit", inx);
			}
		},
		onSelect : function(inx) {
			if (editingRowIndex != undefined) {
				$("#menu_table").datagrid("unselectRow", inx);
			}
		},
		columns : [ [ {
			field : "name",
			title : "目录名称",
			width : 100,
			sortable : true,
			editor : {
				type : "validatebox",
				options : {
					required : true
				}
			}
		}, {
			field : "url",
			title : "链接地址",
			width : 100,
			editor : "text"
		}, {
			field : "position",
			title : "排序索引",
			width : 100,
			sortable : true,
			editor : {
				type : "numberbox"
			},
			formatter : function(value, row, index) {
				if (value > 0)
					return value;
				else
					return 0;
			}
		}, {
			field : "parentId",
			title : "父目录组",
			width : 100,
			formatter : function(value, row, index) {
				var parentMenuName = "无";
				if (row.parentMenu != null)
					parentMenuName = row.parentMenu.name;
				return parentMenuName;
			},
			editor : {
				type : "combobox",
				options : {
					valueField : "id",
					textField : "name",
					url : "manage/listMenuGroup.html"
				}
			}
		}, {
			field : "isUse",
			title : "是否使用",
			width : 100,
			formatter : function(value, row) {
				if (row.isUse == "1")
					return "是";
				else
					return "否";
			},
			editor : {
				type : "combobox",
				options : {
					required : true,
					editable : false,
					data : [ {
						text : "是",
						value : "1"
					}, {
						text : "否",
						value : "0"
					} ]
				}
			}
		} ] ],
	});
	$(".datagrid-header div").css("textAlign", "center");
});